num = int(input())
strnum = str(num)
maxx = 0
k = 0 
x = 0
y = 1
if num > 9999 or num < 1000:
    print("0")
else:
    for _ in range(4):
        for i in range(2, int(strnum[x:y]) - 1):
            if int(strnum[x:y]) % i == 0:
                k = 1
                break
        if k == 0 and maxx < int(strnum[x:y]):
            maxx = int(strnum[x:y])
        k = 0
        x += 1
        y += 1
    x = 0 
    y = 2
    for _ in range(3):
        for i in range(2, int(strnum[x:y]) - 1):
            if int(strnum[x:y]) % i == 0:
                k = 1
                break
        if k == 0 and maxx < int(strnum[x:y]):
            maxx = int(strnum[x:y])
        k = 0
        x += 1
        y += 1
    x = 0 
    y = 3
    for _ in range(2):
        for i in range(2, int(strnum[x:y]) - 1):
            if int(strnum[x:y]) % i == 0:
                k = 1
                break
        if k == 0 and maxx < int(strnum[x:y]):
            maxx = int(strnum[x:y])
        k = 0
        x += 1
        y += 1
    x = 0 
    y = 4
    for i in range(2, int(strnum[x:y]) - 1):
        if int(strnum[x:y]) % i == 0:
            k = 1
            break
    if k == 0 and maxx < int(strnum[x:y]):
        maxx = int(strnum[x:y])
    if maxx == 1:
        print("0")
    else:
        print(maxx)
